package com.weixinli.web.actions;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;
import javax.imageio.stream.FileImageInputStream;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.weixinli.beans.FmMusic;
import com.weixinli.beans.PageInfo;
import com.weixinli.biz.IFmMusicBiz;
import com.weixinli.dao.IFmMusicDao;
import com.weixinli.web.actions.model.JsonObject;
@Controller("BackFmMusicAction")
public class BackFmMusicAction extends ActionSupport implements ModelDriven<FmMusic>{
	private FmMusic fmMusic;
	
	private JsonObject jsonObject;
	private IFmMusicBiz iFmMusicBiz;
	
	private int page;
	private int rows;
	
	public List<File> file;   
	public List<String> fileContentType;
	public List<String> fileFileName;
	public String savePath;
	
	/**
	 * 查询所有信息
	 * @return
	 */
	public String findAllMusic(){
		List<FmMusic> music=iFmMusicBiz.findAll(new PageInfo(page,rows));
		if(music.size()>0){
			jsonObject.setRows(music);
		}
		return "success";
	}
	/**
	 * 添加一个信息
	 * @return
	 */
	public String addFmMusicInfo(){
		FileOutputStream fos=null;
		FileImageInputStream fis=null;
		String addr=null;   //存放图片
		String Aduio=null;  //存放音频
		boolean success=true;
		//对于文件图上的处理
		for(int i=0;i<file.size();i++){
			try {
				addr=new Date().getTime()+""+new Random().nextInt(1000)+fileFileName.get(i);
				fos=new FileOutputStream(getSavePath()+"\\"+addr);
				fis=new FileImageInputStream(file.get(i));
				
				byte[] buffer=new byte[2048];
				int len=0;
				while((len=fis.read(buffer))>0){
					fos.write(buffer,0,len);
				}
				fos.flush();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				success=false;
			} catch (IOException e) {
				e.printStackTrace();
				success=false;
			}finally{
				if(fos!=null){
					try {
						fos.close();
					} catch (IOException e) {
						e.printStackTrace();
						success=false;
					}
				}
				if(fis!=null){
					try {
						fis.close();
					} catch (IOException e) {
						e.printStackTrace();
						success=false;
					}
				}
			}
		}
		fmMusic.setMaddr(savePath+"/"+addr);
		int data=iFmMusicBiz.addFmMusic(fmMusic);
		FmMusic mu=iFmMusicBiz.selectMid(fmMusic);
		if(mu.getMid()>0){
			jsonObject.setObj(mu.getMid());
		}else{
			jsonObject.setObj(0);
		}
		return "success";
	}

	public String updateMusic(){
		FileOutputStream fos=null;
		FileImageInputStream fis=null;
		String Aduio=null;  //存放音频
		boolean success=true;
		//对于文件图上的处理
		for(int i=0;i<file.size();i++){
			try {
				Aduio=new Date().getTime()+""+new Random().nextInt(1000)+fileFileName.get(i);
				fos=new FileOutputStream(getSavePath()+"\\"+Aduio);
				fis=new FileImageInputStream(file.get(i));
				
				byte[] buffer=new byte[2048];
				int len=0;
				while((len=fis.read(buffer))>0){
					fos.write(buffer,0,len);
				}
				fos.flush();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				success=false;
			} catch (IOException e) {
				e.printStackTrace();
				success=false;
			}finally{
				if(fos!=null){
					try {
						fos.close();
					} catch (IOException e) {
						e.printStackTrace();
						success=false;
					}
				}
				if(fis!=null){
					try {
						fis.close();
					} catch (IOException e) {
						e.printStackTrace();
						success=false;
					}
				}
			}
		}
		fmMusic.setmAudio(savePath+"/"+Aduio);
		SimpleDateFormat ft=new SimpleDateFormat("yyyy/MM/dd  HH:mm:ss");
		fmMusic.setMtime(ft.format(new Date()));
		int data=iFmMusicBiz.updateMusic(fmMusic);
		if(data>0){
			jsonObject.setObj(data);
		}else{
			jsonObject.setObj(0);
		}
		return "success";
	}
	public String delMusic(){
		int data=iFmMusicBiz.delFmMusic(fmMusic);
		if(data>0){
			jsonObject.setResult(data);
		}else{
			jsonObject.setResult(0);
		}
		return "success";
	}
	
	public FmMusic getModel() {
			fmMusic=new FmMusic();
			return fmMusic;
	}
	public JsonObject getJsonObject() {
		return jsonObject;
	}
	@Resource(name="jsonObject")
	public void setJsonObject(JsonObject jsonObject) {
		this.jsonObject = jsonObject;
	}
	@Resource(name="iFmMusicBiz")
	public void setIFmMusicBiz(IFmMusicBiz iFmMusicBiz) {
		iFmMusicBiz = iFmMusicBiz;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public List<File> getFile() {
		return file;
	}
	public void setFile(List<File> file) {
		this.file = file;
	}
	public List<String> getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(List<String> fileContentType) {
		this.fileContentType = fileContentType;
	}
	public List<String> getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(List<String> fileFileName) {
		this.fileFileName = fileFileName;
	}
	public String getSavePath() {
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	public void setFmMusic(FmMusic fmMusic) {
		this.fmMusic = fmMusic;
	}
	@Resource(name="iFmMusicBiz")
	public void setiFmMusicBiz(IFmMusicBiz iFmMusicBiz) {
		this.iFmMusicBiz = iFmMusicBiz;
	}
	
}
